#include<stdio.h>
#include<stdlib.h>
#include <string.h>
#include<math.h>
struct part{
	char name[20];
	char password[20];
	}*a;

char n[20],n2[20];
char pass[20],pass2[20];
int time=1;


///MULTIPLICATION METHOD
void hash()
{
	int i,key=0,pos,x;
	float y,b;
	y=(sqrt(5) - 1)/2;
	for(i=0;i<20;i++)
	{
		key=key + 5 * (int)(n[i])+i;
	}
	x=key*y;
	y=key*y;
	b=y-x;
	pos=101*b;
	struct part temp;
	for(i=0;i<101;i++)
	{
		if(a[pos].name[0]=='\0')
		{
			strcpy(a[pos].name,n);		
			strcpy(a[pos].password,pass);
			break;
		}
		else
		{
			pos=pos+1;
		}
	}	
}

void check()
{
	int i,key=0,pos,x;
	int ch=0;
	float y,b;
	y=(sqrt(5) - 1)/2;
	for(i=0;i<20;i++)
	{
		key=key + 5 * (int)(n2[i]) + i;
	}
	x=key*y;
	y=key*y;
	b=y-x;
	pos=101*b;
	for(i=0;i<101;i++)
	{	
		if(!(strcmp(a[pos].name,n2)))
		{
			ch=1;
			if(!(strcmp(a[pos].password,pass2)))
			{
				printf("Correct password \n");
				time=10;
				break;
			}
			else
			{
				printf("Incorrect password \n");
				time++;
				break;
			}
		}
		else
		{
		pos=pos+1;
		}
	}
	if(ch==0)
	{
	printf("Name not found\n");
	time++;
	}
}


void main()
{
	a = malloc(101*(sizeof(struct part)));
	FILE *fp;
	char line[100];
	fp=fopen("account.txt","r");
	int i;
	while(fgets(line, 200,fp))
		{
		sscanf(line,"%s\t%s\n",n,pass);
		hash();	
		}
	fclose(fp);
	while(1){
		if(time < 4){
		printf("Enter the name: ");
		scanf("%s",n2);
		printf("Enter the password: ");
		scanf("%s",pass2);
		check();}
		else if(time==10){
			break;}
		else{
			printf("Too many tries,forbidden access\n");
			break;
		}
	}
}
	
